package com.tinysand.advert.advice;

import com.tinysand.advert.exception.AdvertException;
import com.tinysand.advert.support.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 广告微服务系统中的全局异常处理类
 *
 * Created By TINY
 * Create Time 2020/2/14
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionAdvice {

    @ExceptionHandler(AdvertException.class)
    public CommonResponse<String> handleAdvertException(
            HttpServletRequest request, AdvertException exception) {

        final String msg = String.format("请求（%s）发生异常！",
                request.getRequestURI());

        log.error(msg);
        CommonResponse<String> response =
                new CommonResponse<>(-1, msg);

        response.setData(exception.getLocalizedMessage());
        return response;
    }
}
